import { defineStore } from "pinia"
import { BasicPageSearch } from "@/models/basicType";
import { LivePageResult } from "@/models/live"
import { liveApi } from "@/services"
import { toast } from "vue3-toastify"

const useAllLiveStore  = defineStore('AllLive', {
    // state
    state:() =>({
        pageSize: 16,
        pageNum: 1,
        totalRecords: 0,
        livePageResult: {} as LivePageResult,
    }),
    // actions
    actions:{
        // 获取所有的直播列表（分页）
        async getAllLives(pageSize:number, pageNum:number) {
            try{        
                const data : BasicPageSearch = {
                    pageSize: pageSize,
                    pageNum: pageNum,
                }
                const res = await liveApi.getLives(data)
                this.pageSize = pageSize
                this.pageNum = pageNum
                this.livePageResult = res
                this.totalRecords = this.livePageResult.count             
            } catch(err) {
                toast.error('获取所有的直播列表出现异常：'+ err);
            }
        },

        // 初始化数据
        async initData(){
            await this.refreshData();
        },

        // 刷新数据（在当前页）
        async refreshData(){
            await this.getAllLives(this.pageSize,this.pageNum)
        },

        // 开始查询
        async handleSearch(){      
            await this.getAllLives(this.pageSize,1)
        },

        async handlePageChange(pageNum:number,pageSize:number){     
            this.pageNum = pageNum
            this.pageSize = pageSize   
            this.refreshData()
        }
    },
    getters:{
        liveSource:(state) => state.livePageResult.records,
    }
})

export default useAllLiveStore